package com.gps.skyrc.tool;

import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.View;
import android.view.Window;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.gps.skyrc.R;


/**
 * Created by ${ywq} on 2018/1/1 0001.
 */

public class RenameDialog extends Dialog {

    private EditText mNameEdit;
    private ImageView mCleanIv;
    private TextView mQuxiao;
    private TextView mQueren;
    private onRenameOnclickListener mOnRenameOnclickLlistener;
    private String mName;
    private TextView mTitleName;
    private String titleName;
    public RenameDialog(@NonNull Context context, String name, String titleName) {
        super(context);
        this.mName = name;
        this.titleName = titleName;
    }


    public void setRenameOnclickListener(onRenameOnclickListener onclickListener){
        this.mOnRenameOnclickLlistener = onclickListener;
    }

    public interface onRenameOnclickListener{
        void onRenamecancelClick(View view);

        void onRenameconfirmClick(String name);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setBackgroundDrawable(new BitmapDrawable());
        setContentView(R.layout.rename_dialog);
        //按空白处不能取消动画
        setCanceledOnTouchOutside(false);
        //初始化界面控件
        initView();
        //初始化界面数据
        initData();
        //初始化界面控件的事件
        initEvent();
    }

    private void initView() {
        mNameEdit = findViewById(R.id.name_edit);
        mCleanIv = findViewById(R.id.clean_iv);
        mQuxiao = findViewById(R.id.quxiao_tv);
        mQueren = findViewById(R.id.queren_tv);
        mTitleName = findViewById(R.id.name_tv);
    }

    private void initData() {
        mNameEdit.setText(mName);
        mTitleName.setText(titleName);
    }

    private void initEvent() {
        mCleanIv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mNameEdit.setText("");
            }
        });

        mQuxiao.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mOnRenameOnclickLlistener != null){
                    mOnRenameOnclickLlistener.onRenamecancelClick(view);
                }
            }
        });

        mQueren.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mOnRenameOnclickLlistener != null){
                    mOnRenameOnclickLlistener.onRenameconfirmClick(mNameEdit.getText().toString().trim());
                }
            }
        });
    }
}
